package com.haochang.chunkvod.view.vod.popupWindow;

import java.util.ArrayList;
import java.util.List;

import com.haochang.base.BaseListView;
import com.haochang.base.BaseTextView;
import com.haochang.base.OnBaseClickListener;
import com.haochang.chunkvod.R;
import com.haochang.chunkvod.app.utils.ScreenUtils;
import com.haochang.chunkvod.controller.adapter.base.BaseAdapterHelper;
import com.haochang.chunkvod.controller.adapter.base.QuickAdapter;
import com.haochang.chunkvod.tools.vod.other.DipPxConversion;

import android.content.Context;
import android.graphics.drawable.ColorDrawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.RelativeLayout.LayoutParams;

/**
 * @author qintao
 *         created at 2016/6/3 17:12
 *         title右边弹出的选择操作框
 */
public class VodPopWindow extends PopupWindow
{
	private Context mContext;
	private BaseListView listView;
	private QuickAdapter<String> dataAdapter;
	private QuickAdapter<SecondMenuInfo> secondAdapter;
	private List<String> mTitleList = new ArrayList<String>();
	private List<SecondMenuInfo> secondMenuInfoList = new ArrayList<SecondMenuInfo>();

	private OnPopWindowListListener popWindowListListener;
	private OnPopWindowLeftListListener popWindowLeftListListener;
	private View contentView;
	private LinearLayout leftView;
	private int selectPosition = -1;

	public void setPopWindowListListener(OnPopWindowListListener listener)
	{
		popWindowListListener = listener;
	}

	public void setPopWindowLeftListListener(OnPopWindowLeftListListener listener)
	{
		popWindowLeftListListener = listener;
	}

	public VodPopWindow(Context context, List<String> titleList)
	{
		mContext = context;
		mTitleList = titleList;
		LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		contentView = inflater.inflate(R.layout.view_popwindow, null);
		this.setContentView(contentView);
		this.setWidth(LayoutParams.MATCH_PARENT);
		this.setHeight(LayoutParams.WRAP_CONTENT);
		this.setFocusable(true);
		this.setOutsideTouchable(true);
		ColorDrawable dw = new ColorDrawable(0000000000);
		this.setBackgroundDrawable(dw);
		this.setAnimationStyle(R.style.PopWindow);
		listView = (BaseListView) contentView.findViewById(R.id.listView);
		leftView = (LinearLayout) contentView.findViewById(R.id.pop_div_left);
		leftView.setVisibility(View.GONE);
		dataAdapter = new QuickAdapter<String>(mContext, R.layout.item_popwindow_list_layout, mTitleList)
		{
			@Override
			protected void convert(final BaseAdapterHelper helper, final String item)
			{
				View view = helper.getView();
				ImageView imageView = (ImageView) view.findViewById(R.id.img_menu_left);
				imageView.setVisibility(View.GONE);
				//显示二级菜单标识
				if (item.contains("=sec"))
				{
					imageView.setVisibility(View.VISIBLE);
					helper.setText(R.id.tv_title, item.substring(0, item.lastIndexOf("=")));
				} else
				{
					helper.setText(R.id.tv_title, item);
				}
				helper.getView(R.id.pop_layout).setTag(R.id.common_pop_item, item);
				helper.getView(R.id.pop_layout).setTag(R.id.common_pop_item_position, helper.getPosition());
				helper.getView(R.id.pop_layout).setOnClickListener(onBaseClickListener);
				if (selectPosition == helper.getPosition())
				{
					helper.getView(R.id.pop_layout).setBackgroundColor(mContext.getResources().getColor(R.color.color_eeeeee));
				} else
				{
					helper.getView(R.id.pop_layout).setBackgroundColor(mContext.getResources().getColor(R.color.white));
				}
			}
		};
		listView.setAdapter(dataAdapter);
	}

	private OnBaseClickListener onBaseClickListener = new OnBaseClickListener()
	{
		@Override
		public void onBaseClick(View v)
		{
			switch (v.getId())
			{
			case R.id.pop_layout:
				if (popWindowListListener != null)
				{
					Integer position = (Integer) v.getTag(R.id.common_pop_item_position);
					String item = (String) v.getTag(R.id.common_pop_item);
					popWindowListListener.onSelectItem(position);
					selectPosition = position;
					if (!item.contains("=sec"))
					{
						dismissPopWindow();
					}
					dataAdapter.notifyDataSetChanged();
				}
				break;
			case R.id.pop_sec_layout:
				if (popWindowLeftListListener != null)
				{
					Integer position = (Integer) v.getTag(R.id.common_pop_item_position);
					SecondMenuInfo item = (SecondMenuInfo) v.getTag(R.id.common_pop_item);
					onRefresh(item);
					popWindowLeftListListener.onSelectItem(position);
					dismissPopWindow();
				}
				break;
			default:
				break;
			}

		}
	};

	/**
	 * 显示二级菜单栏
	 * add by 秦涛
	 * @param position 添加到的位置
	 * @param needGou 是否需要显示右侧的勾
	 * @return 添加的左侧菜单栏View
	 */
	public void showSecondMenuView(int position, List<SecondMenuInfo> items, final boolean needGou)
	{
		leftView.setVisibility(View.VISIBLE);
		LayoutParams params = new LayoutParams(DipPxConversion.dip2px(mContext, 160), ViewGroup.LayoutParams.WRAP_CONTENT);
		int topMargin = position * DipPxConversion.dipToPx(mContext, 40) + contentView.findViewById(R.id.pop_arrow).getHeight();//右侧每行的高度*行数+顶部图片的高度
		int rightMargin = contentView.findViewById(R.id.pop_div_right).getWidth() + DipPxConversion.dip2px(mContext, 12 * 2);
		params.setMargins(ScreenUtils.getScreenWidth(mContext) - rightMargin - DipPxConversion.dip2px(mContext, 160), topMargin, rightMargin, 0);
		leftView.setLayoutParams(params);
		BaseListView left_listView = (BaseListView) contentView.findViewById(R.id.left_listView);
		secondMenuInfoList.clear();
		secondMenuInfoList.addAll(items);
		secondAdapter = new QuickAdapter<SecondMenuInfo>(mContext, R.layout.item_popwindow_left_list_layout, secondMenuInfoList)
		{
			@Override
			protected void convert(final BaseAdapterHelper helper, final SecondMenuInfo item)
			{
				helper.getView(R.id.pop_left_gou).setVisibility(View.GONE);
				helper.setText(R.id.tv_title, item.getName());
				if (item.isSelected())
				{
					if (needGou)
					{
						helper.getView(R.id.pop_left_gou).setVisibility(View.VISIBLE);
						((BaseTextView) helper.getView(R.id.tv_title)).setTextColor(mContext.getResources().getColor(R.color.color_f04e39));
					} else
					{
						helper.getView(R.id.pop_left_gou).setVisibility(View.GONE);
						((BaseTextView) helper.getView(R.id.tv_title)).setTextColor(mContext.getResources().getColor(R.color.black));
					}
				} else
				{
					helper.getView(R.id.pop_left_gou).setVisibility(View.GONE);
					((BaseTextView) helper.getView(R.id.tv_title)).setTextColor(mContext.getResources().getColor(R.color.black));
				}
				helper.getView(R.id.pop_sec_layout).setTag(R.id.common_pop_item, item);
				helper.getView(R.id.pop_sec_layout).setTag(R.id.common_pop_item_position, helper.getPosition());
				helper.getView(R.id.pop_sec_layout).setOnClickListener(onBaseClickListener);
			}
		};
		left_listView.setAdapter(secondAdapter);
	}

	/**
	 * 刷新数据
	 *
	 * @param info
	 */
	private void onRefresh(SecondMenuInfo info)
	{
		for (SecondMenuInfo itemInfo : secondMenuInfoList)
		{
			if (itemInfo.getName().equals(info.getName()))
			{
				itemInfo.setSelected(true);
			} else
			{
				itemInfo.setSelected(false);
			}
		}
	}

	/**
	 * 关闭PopWindow
	 */

	public void dismissPopWindow()
	{
		if (this.isShowing())
		{
			this.dismiss();
		}
	}

	/**
	 * 显示popupWindow
	 *
	 * @param parent
	 */
	public void showPopupWindow(View parent, int x)
	{
		if (!this.isShowing())
		{
			this.showAsDropDown(parent, x, 0);
		} else
		{
			this.dismiss();
		}
	}

	/**
	 * pop列表监听
	 */
	public interface OnPopWindowListListener
	{
		void onSelectItem(int position);
	}

	/**
	 * pop列表监听
	 */
	public interface OnPopWindowLeftListListener
	{
		void onSelectItem(int position);
	}
}
